/*
 * @Author: wuqingchun
 * @Date: 2024-05-20 16:58:12
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-05-20 17:48:36
 */
#ifndef __THREADLOCAL_WRAPPER_H__
#define __THREADLOCAL_WRAPPER_H__

#include <pthread.h>
#include <stdlib.h>

template<typename T>
class ThreadLocalWrapper
{
private:
    /* data */
public:
    ThreadLocalWrapper(/* args */)
    {
        if (pthread_key_create(&m_stKey, NULL) == 0)
        {
            m_bOk = true;
        }
    }

    ~ThreadLocalWrapper()
    {
        
    }

    bool IsOk()
    {
        return m_bOk;
    }

    int Set(const T* pstData)
    {
        return pthread_setspecific(m_stKey, pstData);
    }

    T* Get()
    {
        return reinterpret_cast<T*>(pthread_getspecific(m_stKey));
    }

private:
    pthread_key_t m_stKey;
    bool m_bOk = false;
};


#endif //__THREADLOCAL_WRAPPER_H__